/*
 * Cute4jCore.cpp  0.4.0 / Nov 14, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "Cute4jCore.h"

// Cute4jCore :: Local Functions. ----------------------------------------------

void MessageHandler(QtMsgType type, const char *msg)
{
  switch (type) {
    case QtDebugMsg:
      fprintf(stderr, "[Cute4j][DEBUG] %s\n", msg);
      break;
    case QtWarningMsg: {
      JNILogger::logWarning(msg);
      #ifdef CUTE4J_DEBUG
        Cute4j::WarningMessage("Cute4j Internal - Warning Message", msg);
      #endif // CUTE4J_DEBUG
      break;
    }
    case QtCriticalMsg: {
      JNILogger::logError(msg);
      #ifdef CUTE4J_DEBUG
        Cute4j::ErrorMessage("Cute4j Internal - Critical Error", msg);
      #endif // CUTE4J_DEBUG
      break;
    }
    case QtFatalMsg: {
      JNILogger::logError(msg);
      #ifdef CUTE4J_DEBUG
        Cute4j::ErrorMessage("Cute4j Internal - Fatal Error", msg);
      #endif // CUTE4J_DEBUG
      JVMWrapper::exit();
      break;
    }
  }
}

void InitializeQt()
{
  qInstallMsgHandler(MessageHandler);
  if (!qRegisterMetaType<jboolean>("jboolean") ||
      !qRegisterMetaType<jchar>("jchar") ||
      !qRegisterMetaType<jshort>("jshort") ||
      !qRegisterMetaType<jint>("jint") ||
      !qRegisterMetaType<jlong>("jlong") ||
      !qRegisterMetaType<jfloat>("jfloat") ||
      !qRegisterMetaType<jdouble>("jdouble") ||
      !qRegisterMetaType<jsize>("jsize") ||
      !qRegisterMetaType<jobject>("jobject") ||
      !qRegisterMetaType<jstring>("jstring") ||
      !qRegisterMetaType<jclass>("jclass") ||
      !qRegisterMetaType<jbyteArray>("jbyteArray") ||
      !qRegisterMetaType<jobjectArray *>("jobjectArray *") ||
      !qRegisterMetaType<JNIEnv *>("JNIEnv *")) {
    qFatal("MetaType initialization failed.");
  }
}

// Cute4jCore :: OnLoad Handler. -----------------------------------------------

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *)
{
  InitializeQt();
  JVMWrapper::initialize(jvm);
  return CUTE4J_JNI_VERSION;
}
